package com.loongxun.playwithyou.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.ali.ToAliActivity;
import com.loongxun.playwithyou.ui.activity.user.UserOrderActivity;
import com.loongxun.playwithyou.view.MyDialog;

/**
 * 支付宝支付
 * Created by Administrator on 2017/8/3.
 */
public class ToPlayAcitvity extends Activity implements View.OnClickListener{

    private Context context;
    //订单号，描述，价格
    private String orderNo,subject,price;

    private TextView tv_order_no,tv_order_price,tv_heji;
    private CheckBox cb_play_ali,cb_protocol;

    private MyDialog mDialog;
    private Boolean playAli = true,protocol = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_to_play);
        context = ToPlayAcitvity.this;

        orderNo = getIntent().getStringExtra("orderNo");
        subject = getIntent().getStringExtra("subject");
        price = getIntent().getStringExtra("price");

        initView();
        tv_order_no.setText("订单号："+orderNo);
        tv_order_price.setText("支付金额：￥"+price);
        tv_heji.setText("合计：人民币"+price+"元");
    }

    private void initView() {
        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.tv_to_play).setOnClickListener(this);

        tv_order_no = (TextView) findViewById(R.id.tv_order_no);
        tv_order_price = (TextView) findViewById(R.id.tv_order_price);
        cb_play_ali = (CheckBox) findViewById(R.id.cb_play_ali);
        cb_play_ali.setChecked(playAli);
        cb_play_ali.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                playAli = isChecked;
            }
        });
        cb_protocol = (CheckBox) findViewById(R.id.cb_protocol);
        cb_protocol.setChecked(protocol);
        cb_protocol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                protocol = isChecked;
            }
        });
        tv_heji = (TextView) findViewById(R.id.tv_heji);

        findViewById(R.id.tv_protocol).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()){
            case R.id.iv_back:
                cancelOrder();
                break;
            case R.id.tv_to_play:
                if (!playAli) {
                    Toast.makeText(this,"请选择支付方式",Toast.LENGTH_SHORT).show();
                }else if (!protocol) {
                    Toast.makeText(this,"请您在同意完私教协议之后再进行支付",Toast.LENGTH_SHORT).show();
                }else {
                    //去支付宝支付
                    intent = new Intent(context,ToAliActivity.class);
                    intent.putExtra("orderNo", orderNo);
                    intent.putExtra("subject", subject);
                    startActivityForResult(intent, 10);
                }
                break;
            case R.id.tv_protocol:
                intent = new Intent(this,ProtocolActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            if (requestCode == 10) {
                if (data.getBooleanExtra("ALi",false)){
                    okOrder();
                }else {
                    failOrder();
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        cancelOrder();
        return super.onKeyDown(keyCode, event);
    }

    private void cancelOrder() {
        mDialog = new MyDialog(context,"提示","您确定要结束本次交易？");
        mDialog.show();
        mDialog.positive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                Intent intent = new Intent(context, UserOrderActivity.class);
                intent.putExtra("orderState",0);
                startActivity(intent);
                finish();
            }
        });
        mDialog.negative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
    }

    private void okOrder() {
        mDialog = new MyDialog(context,"支付成功","是否继续购物？");
        mDialog.show();
        mDialog.positive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                finish();
            }
        });
        mDialog.negative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                Intent intent = new Intent(context, UserOrderActivity.class);
                intent.putExtra("orderState",1);
                startActivity(intent);
                finish();
            }
        });
    }

    private void failOrder() {
        mDialog = new MyDialog(context,"支付失败","是否继续支付？");
        mDialog.show();
        mDialog.positive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        mDialog.negative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                Intent intent = new Intent(context, UserOrderActivity.class);
                intent.putExtra("orderState", 0);
                startActivity(intent);
                finish();
            }
        });
    }

}
